﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Utility
{
    public class GenerateGuid
    {
        public GenerateGuid()
        {

        }

        public static string NewGuidNo()
        {
            return Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
        }

        public static T[] RandomSort<T>(T[] array)
        {
            int len = array.Length;
            List<int> list = new List<int>();
            T[] ret = new T[len];
            Random rand = new Random();
            int i = 0;
            while (list.Count < len)
            {
                int iter = rand.Next(0, len);
                if (!list.Contains(iter))
                {
                    list.Add(iter);
                    ret[i] = array[iter];
                    i++;
                }

            }
            return ret;
        }

        public  static DataTable SelectTop(int top, DataTable dt)
        {
            if (dt.Rows.Count < top) return dt;

            DataTable newTable = new DataTable();

            int columns = dt.Columns.Count;
            string[] col = new string[columns];

            //取得要筛选表的所有列名
            for (int c = 0; c < columns; c++)
            {
                col[c] = dt.Columns[c].ColumnName;
            }

            //创建新表的结构
            foreach (string columnName in col)
            {
                newTable.Columns.Add(columnName);
            }

            //选取所有行
            DataRow[] rows = dt.Select("1=1");
            DataRow newRow;
            for (int i = 0; i < top; i++)
            {
                newRow = newTable.NewRow();
                foreach (string columnName in col)
                {
                    newRow[columnName] = rows[i][columnName].ToString();
                }

                newTable.Rows.Add(newRow);
            }
            dt.Dispose();
            return newTable;
        }
    }
}
